package jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * 用户登录功能
 *
 * 程序启动后，要求用户在控制台上输入用户名与密码，
 * 用该用户名和密码作为过滤条件去user表中查询对应的记录
 * 如果查询到记录且一致，则登录成功，否则登录失败
 *
 * SELECT id,username,password,nickname,age
 * FROM user
 * WHERE username = '用户输入的用户名' AND password = '用户输入的密码'
 */
public class LoginDemo {
    public static void main(String[] args) {
        System.out.println("欢迎登录！");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
        System.out.println("用户名: " + username + ", 密码: " + password);
        try (Connection connection = DBUtil.getConnection()) {
            Statement statement = connection.createStatement();
            String sql = "SELECT id, username, password, nickname, age " +
                    "FROM user " +
                    "WHERE username = '"+username+"' AND password = '" +password+"'";
            ResultSet rs= statement.executeQuery(sql);

                    if (rs.next()) {
                        System.out.println("登录成功！");
                    } else {
                        System.out.println("登录失败！");
                    }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

